package tester;

import java.util.Scanner;
import java.util.regex.Pattern;

abstract class Question {
	private String questionText;
	private String questionOutro;
	private String[] answers;
	protected String regexp;
	
	Question (String questionText, String questionOutro, 
			String regexp, String[] answers) {
		this.questionOutro = new String(questionOutro);
		this.questionText = new String(questionText);
		this.regexp = new String(regexp);
		this.answers = new String [answers.length];
		System.arraycopy(answers, 0, this.answers, 0, answers.length);
	}
	
	void printQuestion() {
		System.out.println(questionText);
	}
	
	void printQuestionOutro() {
		System.out.println();
		for (int i = 0; i < answers.length; i++) {
			System.out.println((i + 1) + ") " + answers[i]);
		}
		System.out.println(questionOutro);
	}
	
	void printResult(boolean result) {
		System.out.print("Your answer was ");
		if (!result) {
			System.out.print("in");
		}
		System.out.println("correct!");
		System.out.println();
	}
	
	protected String readAnswer() {
		Scanner input = new Scanner(System.in);
		String result = input.nextLine();
		input.close();
		return result;
	}
	
	protected boolean validateAnswer(String regexp, String answer) {
		return Pattern.matches(regexp, answer);
	}
	
	abstract protected boolean checkAnswer(String answer);
	
	public void askQuestion() {
		this.printQuestion();
		this.printQuestionOutro();
		String answer = readAnswer();
		this.printResult(checkAnswer(answer));
	}
}
